package com.extension.company.model.home;

import android.content.Context;
import android.util.Log;

import com.extension.company.contract.home.SearchContract;
import com.extension.company.dao.daoUtil.DBOperationUntil;
import com.extension.company.greenDaoDb.SearchHistoryDb;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：zwk
 * 时间：on 2019/3/18 13:57
 * 功能：search的mode
 */
public class SearchMode implements SearchContract.Mode {

	private DBOperationUntil dbOperationUntil;

	@Override
	public void requestSearchData(Context context, final SearchContract.searchAllCallBack callBack) {
		if (dbOperationUntil == null) {
			dbOperationUntil = new DBOperationUntil(context);
		}

		Observable.create(new ObservableOnSubscribe<List<SearchHistoryDb>>() {

			@Override
			public void subscribe(ObservableEmitter<List<SearchHistoryDb>> emitter) throws Exception {
				List<SearchHistoryDb> list = dbOperationUntil.listAll();
				emitter.onNext(list);
				Log.d("zwk", Thread.currentThread().getName());

			}
		}).subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<List<SearchHistoryDb>>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(List<SearchHistoryDb> DbList) {
						for (int i = 0; i < DbList.size(); i++) {
							DbList.get(i).getId();
							Log.d("zwk", "id号是" + DbList.get(i).getId());
						}

						callBack.searchAllCallBackSuccess(DbList);

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});

	}

	@Override
	public void insertSearchData(Context context, final String searchStr, final SearchContract.insertCallBack callBack) {

		if (dbOperationUntil == null) {
			dbOperationUntil = new DBOperationUntil(context);
		}

		final SearchHistoryDb searchHistoryDb = new SearchHistoryDb();
		searchHistoryDb.setName(searchStr);

		Observable.create(new ObservableOnSubscribe<String>() {
			@Override
			public void subscribe(ObservableEmitter<String> emitter) throws Exception {

				if (dbOperationUntil.qureyContentCount(searchStr) < 1) {  //  先判断是否有这条记录，没有才插入
					boolean flag = dbOperationUntil.insert(searchHistoryDb);
					Log.d("zwk", "插入是否成功" + flag);
					if (flag) {
						emitter.onNext("插入成功");
					} else {
						emitter.onError(null);
					}
				}

			}
		}).subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<String>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(String s) {
						callBack.insertCallBackSuccess(true);
					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});


	}

	@Override
	public void deleteAll(Context context, final SearchContract.deleteaAllCallBack callBack) {
		Observable.create(new ObservableOnSubscribe<Boolean>() {
			@Override
			public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
				boolean flag = dbOperationUntil.deleteAll();
				if (flag) {
					emitter.onNext(true);
				} else {

					emitter.onNext(false);
				}

			}
		}).observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<Boolean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(Boolean aBoolean) {
						if (aBoolean) {
							callBack.deleteAllCallBackSuccess(true);
						} else {
							callBack.deleteAllCallBackFaile(false);
						}


					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}
}
